package com.cpi.cornpp.instructionsattachment.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.upload.FormFile;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.creditline.exception.CreditLineException;
import com.cpi.cornpp.instructionsattachment.converter.InstructionAttachmentConverter;
import com.cpi.cornpp.instructionsattachment.dao.InstructionAttachmentDAO;
import com.cpi.cornpp.instructionsattachment.dto.InstructionAttachmentDTO;
import com.cpi.cornpp.instructionsattachment.exception.InstructionAttachmentException;
import com.cpi.cornpp.instructionsattachment.view.InstructionAttachmentView;
import com.cpi.cornpp.purchaseinstructiondetail.view.InstructionPurchaseDetailView;
import com.cs.common.init.SystemParameterCollection;

public class InstructionAttachmentService {

	public List list(int purchaseInstructionDetailID)throws InstructionAttachmentException{
		InstructionAttachmentDAO dao = null;
		ArrayList list = null;
		ArrayList aux = null;
		InstructionAttachmentConverter converter = null;
		
		dao = new InstructionAttachmentDAO();
		list = new ArrayList();
		aux = new ArrayList();
		converter = new InstructionAttachmentConverter();
		try{
			aux = (ArrayList)dao.getInstructionAttachement(purchaseInstructionDetailID);
			list = (ArrayList)converter.convertDTOs(aux);			
			//list.addAll(converter.convertDTOsToViews(this.help()));//TODO REMOVE ME WHEN TEST IS DONE
		}catch (InstructionAttachmentException instructionAttachmentException) {			
			instructionAttachmentException.printStackTrace(System.out);
			instructionAttachmentException= new InstructionAttachmentException(instructionAttachmentException , InstructionAttachmentException.LAYER_SERVICE,InstructionAttachmentException.ACTION_LISTS);
			throw instructionAttachmentException;

		}
		catch (Exception e){
			InstructionAttachmentException instructionAttachmentException=null;
			e.printStackTrace(System.out);
			instructionAttachmentException= new InstructionAttachmentException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionAttachmentException;
		}
	
		return list;
	}
	
	public InstructionAttachmentView getInstructionAttachmentById(int instructionAttachmentId)throws InstructionAttachmentException{
		InstructionAttachmentDAO dao = null;
		InstructionAttachmentConverter converter = null;
		InstructionAttachmentDTO dto = null;
		InstructionAttachmentView view = null;
		dao = new InstructionAttachmentDAO();		
		converter = new InstructionAttachmentConverter();
		try{
			dto = dao.getSingleInstructionAttachement(instructionAttachmentId);
			view = converter.convert(dto);			
		}catch (InstructionAttachmentException instructionAttachmentException) {			
			instructionAttachmentException.printStackTrace(System.out);
			instructionAttachmentException= new InstructionAttachmentException(instructionAttachmentException , InstructionAttachmentException.LAYER_SERVICE,InstructionAttachmentException.ACTION_LISTS);
			throw instructionAttachmentException;

		}
		catch (Exception e){
			InstructionAttachmentException instructionAttachmentException=null;
			e.printStackTrace(System.out);
			instructionAttachmentException= new InstructionAttachmentException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionAttachmentException;
		}
	
		return view;
	}

	/**
	 * Inserta un instruction forecast
	 * @author Edgar Joao
	 * @param view
	 * @throws CreditLineException
	 */
	public void addInstructionAttachmentView(InstructionAttachmentView view) throws InstructionAttachmentException{
		InstructionAttachmentDAO dao = null;
		InstructionAttachmentConverter converter = null;
		InstructionAttachmentDTO dto = null;		
		if(view != null){
			dao = new InstructionAttachmentDAO();
			converter = new InstructionAttachmentConverter();
			dto = converter.convert(view);			
			try {
				dao.insertInstructionAttachement(dto);
				
			} catch (InstructionAttachmentException ife) {
				throw ife;
			} catch (Exception e) {
				InstructionAttachmentException ife = new InstructionAttachmentException(e,InstructionAttachmentException.LAYER_SERVICE,	InstructionAttachmentException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw ife;
			}				
		}
	}
	
	/**
	 * Elimina un Instruction attachment view
	 * @param id
	 * @throws InstructionAttachmentException
	 */
	public void deleteInstructionAttachmentView(int id)throws InstructionAttachmentException{
		InstructionAttachmentDAO dao = null;
		try {
			dao = new InstructionAttachmentDAO();
			dao.deleteInstructionsView(id);
		} catch (InstructionAttachmentException e) {
			throw e;
		}
	}
	
	/**
	 * Actualiza un Instruction attachment view
	 * @param views
	 * @param checked
	 * @throws InstructionAttachmentException
	 */
	public void updateInstructionAttachmentView(InstructionAttachmentView view) throws InstructionAttachmentException{
		InstructionAttachmentDAO dao = null;
		InstructionAttachmentConverter converter = null;
		InstructionAttachmentDTO dto = null;
		
		if(view != null){
			dao = new InstructionAttachmentDAO();
			converter = new InstructionAttachmentConverter();
			dto = converter.convert(view);			
			try {
				dao.updateInstructionAttachment(dto);				
			} catch (InstructionAttachmentException pe) {
				throw pe;
			} catch (Exception e) {
				InstructionAttachmentException pe = new InstructionAttachmentException(e,
						InstructionAttachmentException.LAYER_SERVICE,
						InstructionAttachmentException.ACTION_UPDATE);
				e.printStackTrace(System.out);
				throw pe;
			}
		}		
	}
	
	/**
	 * Arma el path completo donde se guardar el archivo
	 * @param ipdView 
	 * @param fileName
	 * @return
	 */
	public String buildPath(InstructionPurchaseDetailView ipdView, String fileName){
		String path = SystemParameterCollection.getInstance().getSystemParameterBean("PATH_LOCATION_FOR_FILES").getValue();
		int paddingId = SystemParameterCollection.getInstance().getSystemParameterBean("INSTRUCTION_PURCHASE_REFERENCE_LENGTH_CODE").getIntValue();		
		int paddingDetailId = SystemParameterCollection.getInstance().getSystemParameterBean("INSTRUCTION_PURCHASE_DETAIL_REFERENCE_LENGTH_CODE").getIntValue();
		StringBuffer fullPath =  new StringBuffer(path);
		fullPath.append(StringUtils.leftPad(String.valueOf(ipdView.getPurchaseInstructionId()),paddingId,"0"));
		fullPath.append("-");
		fullPath.append(StringUtils.leftPad(String.valueOf(ipdView.getPurchaseInstructionDetailid()),paddingDetailId,"0"));
		fullPath.append("-");
		fullPath.append(fileName);		
		return fullPath.toString();
	}
	
	
	public void saveFileToPath(String path, FormFile formFile) throws IOException, InstructionAttachmentException{
		File file = new File(path);
		if(!(new File(file.getParent()).exists())){
			throw new InstructionAttachmentException("label.instructionattachment.directoryerror", InstructionAttachmentException.LAYER_SERVICE, InstructionAttachmentException.ACTION_UPDATE);
		}
		FileOutputStream outputStream = new FileOutputStream(new File(path));						

		outputStream.write(formFile.getFileData());
		outputStream.flush();
		outputStream.close(); 
	}
	


}
